package com.shushan.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.shushan.base.Constants;
import com.shushan.db.RelaxTimeDao;
import com.shushan.home.TabHomeActivity;
import com.shushan.manager.bean.RelaxTimeBean;
import com.shushan.util.ControlUtil;
import com.shushan.util.SharedPreferencesUtils;
import com.shushan.util.SystemUtils;
import com.shushan.util.ToolUtils;

import java.util.List;

/**
 * @ClassName: AutoStartReceiver
 * @Desciption: 自启动广播接收器
 * @author: yichaohua
 * @date: 2018-05-23
 */
public class AutoStartReceiver extends BroadcastReceiver {

    private static final String TAG = "AutoStartReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(TAG,"onReceive()");

        boolean isChildControl = SharedPreferencesUtils.getBooleanDate(Constants.IS_CHILD_CONTROL_SHAREPREFERS_KEY);
        if(isChildControl){
            SystemUtils.sendForegActivity(context,TabHomeActivity.class);
        }

        SharedPreferencesUtils.setStringDate(Constants.FREE_GAME_PSW_SHAREPREFERS_KEY,"");
        SharedPreferencesUtils.setStringDate(Constants.FREE_GAME_START_JSON_SHAREPREFERS_KEY,"");

        SharedPreferencesUtils.setBooleanDate(Constants.LOCK_CHILD_PSW_SHAREPREFERS_KEY,false);

        regainAlarm(context);
    }

    private void regainAlarm(Context context) {
        RelaxTimeDao relaxTimeDao = new RelaxTimeDao(context);
        List<RelaxTimeBean> relaxTimeBeanList = relaxTimeDao.queryForAll();
        if(ToolUtils.isListEmpty(relaxTimeBeanList)){
            return;
        }

        for (RelaxTimeBean relaxTimeBean : relaxTimeBeanList){
            if(relaxTimeBean.isAction){
                ControlUtil.setTimeMgAlarm(context,relaxTimeBean.id,relaxTimeBean.frequDay,relaxTimeBean.startHour,
                        relaxTimeBean.startMin,relaxTimeBean.endHour,relaxTimeBean.endMin);
            }
        }
    }
}
